home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18294 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Calling the wrong constructor
  5. Date: Fri, 19 Apr 1996 17:12:09 -0400
  6. Organization: Datalytics, Inc
  7. Message-ID: <317801A9.4B35@datalytics.com>
  8. References: <4kot87$796@earth.njcc.com> <Andrew_Carol-1804962001240001@17.127.18.252>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Andrew wrote:
  16. > In article <4kot87$796@earth.njcc.com>, mike@pluto.njcc.com (Michael
  17. > Hohenshilt) wrote:
  18. > >   This might seem like a basic question, but lets say you something set u
  19. > > like the following:
  20. > >
  21. > > class Parent { Parent() { allocsomething} ~Parent() { deletesomething }... };
  22. > > class Foo : Parent { Foo() { allocsomething } ~Foo() {deletesomething }... };
  23. > > class Contain { Parent *ptr; ... };
  24. > >
  25. > > both parent, and foo allocate memory, and will free it up when being
  26. > > destructed.  Contain just holds a pointer of type Parent, and its
  27. > > constructor takes such a pointer as a parameter.
  28. > >   If I store a pointer of type Foo into contain.ptr and discard that
  29. > > pointer.  Is there any way to have contain (via destructors) free up all
  30. > > memory allocated by Foo and/or Parent?
  31. > Assuming that "Contain" keeps a copy of the 'ptr'...
  32. > ~Contain() { delete ptr; }
  33. > This will call the destructor of Foo and Parent...
  34.  
  35. Assuming Contain::ptr actually points to a Foo rather than a 
  36. Parent, you need virtual destructors in Parent and Foo.  When 
  37. you do this, the destructor of Foo will be invoked through the 
  38. virtual function mechanism (vtable).  It, in turn, will call 
  39. Parent's destructor.
  40.  
  41. -- 
  42. Robert Stewart        | My opinions are usually my own.
  43. Datalytics, Inc.    | stew@datalytics.com
  44.